package com.wteam.framework.modules.member.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.sql.Order;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.wteam.framework.common.enums.ResultCode;
import com.wteam.framework.common.enums.ResultUtil;
import com.wteam.framework.common.exception.ServiceException;
import com.wteam.framework.common.utils.JwtUtils;
import com.wteam.framework.common.utils.UuidUtils;
import com.wteam.framework.common.vo.ResultMessage;
import com.wteam.framework.modules.community.dto.MemberComputeDto;
import com.wteam.framework.modules.community.entity.SdModel;
import com.wteam.framework.modules.community.mapper.SdModelMapper;
import com.wteam.framework.modules.community.service.SdModelService;
import com.wteam.framework.modules.file.mapper.FileMapper;
import com.wteam.framework.modules.file.service.FileService;
import com.wteam.framework.modules.member.entity.entity.SdMember;
import com.wteam.framework.modules.member.mapper.mapper.SdMemberMapper;
import com.wteam.framework.modules.member.service.SdMemberService;
import com.wteam.framework.modules.system.entity.dos.Setting;
import com.wteam.framework.modules.system.entity.dto.ComputeSetting;
import com.wteam.framework.modules.system.entity.enums.SettingEnum;
import com.wteam.framework.modules.system.entity.vo.ComputeSettingVo;
import com.wteam.framework.modules.system.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * (SdMember)表服务实现类
 *
 * @author doncic
 * @since 2023-07-17 13:35:15
 */
@Service("sdMemberService")
public class SdMemberServiceImpl extends ServiceImpl<SdMemberMapper, SdMember> implements SdMemberService {


    @Autowired
    private SdModelMapper sdModelMapper;
    @Autowired
    private SdModelService sdModelService;

    @Autowired
    private SdMemberMapper sdMemberMapper;
    @Autowired
    private SdMemberService sdMemberService;
    @Autowired
    private SettingService settingService;
    @Autowired
    private FileMapper fileMapper;

    @Override
    public ResultMessage getMemberDetails(Long id) {
        //先校验参数
        if (id == null && ObjectUtil.isEmpty(sdMemberMapper.selectById(id))) {
            return ResultUtil.error(ResultCode.NOT_CREATE_MEMBER);
        }
        LambdaQueryWrapper<SdMember> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SdMember::getId, id);
        List<SdMember> members = sdMemberMapper.selectList(lambdaQueryWrapper);
        return !members.isEmpty()
                ? ResultUtil.data(members)
                : ResultUtil.error(ResultCode.NOT_CREATE_MEMBER);
    }

    @Override
    @Transactional
    public String mobilePhoneLogin(String phoneNumber) {
        long uuid = IdUtil.getSnowflake(0, 0).nextId();
        String nickname = "用户" + uuid;
        QueryWrapper<SdMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone_number", phoneNumber);
//        LambdaQueryWrapper<SdMember> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(SdMember::getPhoneNumber, mobilePhone);
        SdMember member = sdMemberMapper.selectOne(queryWrapper);

        //如果手机号不存在则自动注册用户
        if (member == null) {
            member = new SdMember();
            member.setPhoneNumber(phoneNumber);
            member.setCreateBy(UuidUtils.getUUID());
            member.setCreateTime(new Date());
            member.setIsMember(false);
            member.setNickName(nickname);
            member.setComputePower(0L);
            member.setPhotoCount(4L);

            sdMemberService.save(member);
            return JSONUtil.toJsonStr(member);


        }
        return JSONUtil.toJsonStr(member);


    }




    @Override
    public ResultMessage updateMember(Long id, SdMember sdMember) {
        //先校验参数
        return ResultUtil.data(sdMember);
    }

    //TODO
    @Override
    public void blockUser(Long uuid,String phoneNumber) {

        //这里是写死的
        if(phoneNumber.equals("15917755796")){
            QueryWrapper<SdMember> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", uuid);
//        LambdaQueryWrapper<SdMember> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(SdMember::getPhoneNumber, mobilePhone);
            SdMember member = sdMemberMapper.selectOne(queryWrapper);
            member.setBlock(true);
            sdMemberService.updateById(member);

        }else {
            throw new ServiceException(ResultCode.SUPERMEMBER_BLOCK_ERRO);
        }

    }

    @Override
    public void modelOrder(Long uuid, String phoneNumber, Long order) {
        //这里是写死的
        if(phoneNumber.equals("15917755796")){
            QueryWrapper<SdModel> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", uuid);
//        LambdaQueryWrapper<SdMember> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(SdMember::getPhoneNumber, mobilePhone);


            SdModel model = sdModelMapper.selectOne(queryWrapper);
            model.setOrder(order);
            sdModelService.updateById(model);



        }else {
            throw new ServiceException(ResultCode.SUPERMEMBER_BLOCK_ERRO);
        }
    }


}



